package com.snail.readyvr.application;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.github.pwittchen.networkevents.library.NetworkEvents;
import com.github.pwittchen.networkevents.library.event.ConnectivityChanged;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.snail.readyvr.R;
import com.snail.readyvr.di.component.ApplicationComponent;
import com.snail.readyvr.di.component.DaggerApplicationComponent;
import com.snail.readyvr.di.module.ApplicationModule;
import com.snail.readyvr.util.AppUtils;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.DefaultBusWrapper;
import com.snail.readyvr.util.LogUtils;
import com.snail.readyvr.util.Utils;

import party.loveit.download.core.DLManager;


public class MyApplication extends MultiDexApplication {
    private static final String TAG = "MyApplication";
    private static Context mContext;
    private ApplicationComponent mApplicationComponent;

    private Tracker mTracker;

    /**
     * Gets the default {@link Tracker} for this {@link Application}.
     * @return tracker
     */
    synchronized public Tracker getDefaultTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
            mTracker = analytics.newTracker(R.xml.global_tracker);
            mTracker.setSessionTimeout(300L);
            mTracker.enableExceptionReporting(true);
        }
        return mTracker;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        LogUtils.isDebug = false;//是否开启日志
        //判断当前进程，根据不同进程初始化数据
        String processName = AppUtils.getCurrentProcessName(this);
        LogUtils.d(TAG, "onCreat--》" + processName);
        if (processName != null && processName.equals(this.getPackageName()))
            init();

    }

    private void init() {
        LogUtils.d(TAG, "init()--》");
        initDownload();
        DefaultBusWrapper.getInstance().register(this);
        new NetworkEvents(this, DefaultBusWrapper.getInstance())
                .enableInternetCheck()
                .register();//初始化网络状态变化监控
        mApplicationComponent = DaggerApplicationComponent
                                .builder()
                                .applicationModule(new ApplicationModule(this))
                                .build();

    }

    private void initDownload() {
        DLManager.init(this);
        DLManager.getInstance().setSavePath(Environment.getExternalStorageDirectory() + "/readyvr/videos/");
        DLManager.getInstance().setMaxTask(2);
        DLManager.getInstance().setDebugEnable(true);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag(Constant.BusTag.NET_WORK_TAG)})
    public void onEventMainThread(ConnectivityChanged event) {

        Utils.sConnectivityStatus = event.getConnectivityStatus();
        Log.d(TAG, Utils.sConnectivityStatus.name());
        /*if (!Utils.hasConnected(event.getConnectivityStatus()))
            ToastUtils.show(this, this.getString(R.string.no_network));*/
    }

    public static Context getmContext() {
        return mContext;
    }

    public ApplicationComponent getApplicationComponent() {
        return mApplicationComponent;
    }

}
